سفارش تبلیغ
صبا ویژن

web development

Application resources available in Xamarin. Android

    نظر

after sending the user request to launch the application , it refers to the resources available on the operating system so that they can launch and launch the application. Applications have user interface layouts that exist in XML files.

Applications available on the Android OS have resources that we will teach you in this tutorial.

Application resources available in Xamarin. Android

Basics of application resources in Xamarin.Android

When an application is created for the Android operating system, the resources related to that application are set by the default Xamarin.Android template.

The files that create the default resources are as follows:

icon:

This file has the default Icon.png for the application icon created.

Main.axml:

This file has the default user interface layout for the created application .

Note:

Android supports the xml format while Xamarin.Android uses the axml format.

Strings.xml:

Creates a string table that is used to help localize Word applications.

AboutResources.txt:

This file is not necessary and you can even delete this file because it does nothing special, it just provides an overview of the level of Resources files.

Resource.designer.cs:

This file, unlike the AboutResources.txt file, is very important and is produced by Xamarin.Android . This file has a unique ID"s for each source. In fact, this file was created automatically by Xamarin.Android and will be restored from time to time if needed.

How to create resources and access them?

Creating application resources is a very simple and easy task. Add Strings.xml to the file and project. Once added, Build Action is automatically set to AndroidResource. AndroidResource setting is done by Xamarin.Android tool. Doing so will allow you to properly compile the Xamarin.Android tool and embed the resources into the APK file.

Note:

If Build Action is not set up properly on Android Resource, all existing file in the APK will be removed and no attempt to set or upload files will be effective. In this case, you will encounter a runtime error and eventually the application will crash.

Unlike Android, Xamarin.Android is not strict and supports all lowercase and uppercase characters. And you can easily use uppercase or lowercase letters when naming them because Xamarin.Android does not give any error for using uppercase and lowercase letters.

Note:

When naming sources to separate if you use dashes or spaces are not supported and processed. You can use the underline to separate them.

How to access resources added to the file?

There are two ways to use the resources added to the file:

- Use XML files
- Coding to access resources

Access to added resources through coding

To access the available resources by programming method , we must assign a valid and unique identifier for each resource. The unique identifier assigned to each resource is an integer that will be defined within the Resource class.

The code inside the Resource.designer.cs file gives access to the resources, the code inside the Resource.designer.cs file must be as follows to be able to access the added resources:

public partial class Resource
{
public partial class Attribute
{
}
public partial class Drawable {
public const int Icon = 0x7f020000;
}
public partial class Id
{
public const int Textview = 0x7f050000;
}
public partial class Layout
{
public const int Main = 0x7f030000;
}
public partial class String
{
public const int App_Name = 0x7f040001;
public const int Hello = 0x7f040000;
}
}

By entering this code, you are actually assigning a valid identifier to each resource, although the identifiers assigned to the resource are related to the type of resource. For example, when Icon.png was added to the project, the Resource class was updated by Xamarin.Android.

Resource should not be changed manually, as all changes made to it will be replaced by Xamarin. Android .

To access the resources created using the coding method, you can also use the code below:

@ [<Package Name>.] Resource. <Resource Type>. <Resource Name>

PackageName:

A package whose job is to provide a resource.

ResourceType:

A type of resource that is distributed as described within the Resource class.

Resource Name:

is the name of the source that the source name must be without extension.

Access added resources via XML file

The resources inside the XML file are as follows:

@ [<PackageName>:] <ResourceType> / <ResourceName>.

PackageName:

A package whose job is to provide a resource.

ResourceType:

A type of resource that is distributed as described within the Resource class.

Resource Name:

is the name of the source that the source name must be without extension.

The code inside the Main.axml file to access resources via the XML file is as follows:

Check if the contents of the Main.axml file are not the same as the code below, replace the code below with the contents of the Main.axml file.

<? xml version = "1.0" encoding = "utf-8"? >
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
android: orientation = "vertical"
android: layout_width = "fill_parent"
android: layout_height = "fill_parent">
<ImageView android: id = "@ + id / myImage"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content"
android: src = "@ drawable / flag" />
</ LinearLayout>

In this code, ImageView sets the src property to @drawable / flag.

Learn how to set the default resources of applications in Xamarin.Android

Default resources are resources that are not specific to devices and applications , in fact there are specific resources for each application or device that will be selected by default if specific resources are not found by the Android operating system. Default resources exist from the beginning, and if device-specific resources do not run, default resources will run instead of specific resources.

The different types of sources are as follows:

Animator:

XML files are used to describe property animations. property animations for version 3.0 of Android was introduced and launched. property animations are flexible and are a powerful way to produce different types of animations that are used.

Anim:

XML files are used to describe tween animations. tween animations are actually a set of animation commands that are used to make changes to the contents of the View object. For example, rotating and moving images or resizing text in animation uses Tween animations to make changes inside View objects.

color:

XML files that are used to describe a list of colors. Consider an example of changing the color or using the colors in the list, changing the button mode and changing its color. A button can have active or inactive modes, or it can have compressed or non-compressed modes, in which case the color of the button will change as well.

Drawable resource:

In general, it can be said that drawable is a concept used for graphics. Anything related to graphics in the program can be drawable inside. The drawable can be easily compiled within the program, then called by the API or referenced using other XML resources. bitmaps (.png, .gif, .jpg), bitmaps are examples of drawable that can also be resized. Such as state lists and Nine-Patches ...

Layout:

XML files that are used to describe the interface layout, such as an activity or a row in a list.

menu:

XML files that are used to describe program menus such as Context Menu, Options Menu and submenus. Popup Menu Demo and Standard Controls are examples of in-app menus.

Raw:

These are files that are automatically saved in binary type in raw.

values:

are XML files that have simple values. An XML file in the values ??directory does not define a single source, but several sources.

xml:

are XML files that function similarly to the .NET configuration.

Learn how to set up alternative sources for apps on Xamarin.Android

Alternate resources are resources that are used by the Android operating system in the absence of default resources. If Android is looking for a default source but finds a source that is more accurate than the default source, it will select and run a more accurate source. Of course, if Android does not find an alternative source that is more accurate than the default source, the same default source will be downloaded.

Alternative resources are placed within resource files, such as the default resources, as subcategories of the default resources:

<ResourceType> - <Qualifier>

Qualifier:

A name used to identify the device configuration. There may be more than one qualifier with one name, in which case the names are separated by a hyphen.

The rules that must be followed when adding Qualifiers to resources are as follows:

- There may be more than one qualifier with a name, each of which is separated by a dash.
- The qualifier may be specified only once.

The different types of qualifiers that are mentioned for resources are as follows:

MNC and MCC:

MCC is used to provide and configure the SIM card while MNC provides the device connected to the network. The values ??in MNC and MCC are different for other countries and sources. For example, when assigning a qualifier to a country in Germany, the qualifier must be mcc262. When assigning a qualifier to the United States, the qualifier must be mcc310-mnc026. For information on country codes, you can visit http://mcclist.com/.

Smallest Width:

Used to specify the smallest width of the page intended to run the application.

Available Width:

The minimum width of the page that is intended to display the application and the value of this can be changed by rotating the device.

Available Height:

???????The minimum height of the screen that is intended to display the application and the amount of this can be changed by rotating the device.

Screen Aspect:

This qualifier is based on the dimensions of the screen, not on the orientation, and does not change with the rotation of the device. The values ??that can be included in this qualifier are notlong and long.

Screen Orientation:

This qualifier can be changed by rotating the device and the values ??that are placed in it can be landscape or Portrait.

Dock Mode:

??????? is a qualifier that can take car or desk values.

Night Mode:

??????? This qualifier is used to determine whether the program is in use during the day or at night. The purpose of this qualifier is to give users the opportunity to use dark or light modes of user interfaces. The values ??in this qualifier are: notnight and night.

Screen Pixel Density:

Used to specify the number of pixels at a point on the screen.

The values ??in this qualifier are as follows:

ldpi:

Screen with low density and number of pixels

mdpi:

Screen with medium density and number of pixels

hdpi:

Screen with high density and number of pixels

xhdpi:

Screen with very high density and number of pixels

nodpi:

Resources that have no value

Touchscreen Type:

Used to determine the touch of the screen and the values ??in this qualifier can be notouch, stylus and finger. The finger is used to specify the touch screen, the stylus is used to define the screen using the stylus, and the notouch is used to specify the non-touch screens.

Keyboard Availability:

Used to determine the type of keyboard used, and its values ??may change by changing different types of keyboards. The values ??that this qualifier can take are as follows:

Keys exposed:

In this case, the device has an available and active keyboard, and if there is no software keyboard, the hardware keyboard will be launched.

keyshidden:

In this case, the device has a hardware keyboard, but the hardware keyboard of this device is hidden and of course no software keyboard is active.

keyssoft:

In this case, the device has a software keyboard.

Primary Text Input Method:

This qualifier is used to specify what kind of hardware keys are used for input. The values ??in this qualifier are as follows:

nokeys:

There are no hardware keys to log in to the app .

Qwerty:

There is a qwerty keyboard.

12key:

There is a hardware keyboard that has 12 hardware keys to enter.

Navigation Key Availability:

Activated when there are 5 directions and of course the values ??in this qualifier are as follows:

nav exposed:

Navigation and directional keys are active for users to use.

Navhidden:

Navigation keys are disabled for use by users.

How does Android determine which source to use?

In the Android OS, the applications that are produced have a lot of resources and in order for the applications to be launched, they must be run on a source. The number of resources available in Android is high and the question is how the Android operating system determines which source to use.

Android determines the resources bases as follows:

Eliminate contradictory qualifiers:

In this case, if the device orientation is portrait, all directories in the landscape source will be rejected.

Ignore qualifiers not supported:

Not all qualifiers are available for all API levels. If the resource directory has a qualifier that is not supported by the device, that resource directory will be ignored.

 Identify the next highest priority qualifier:

Selects the highest qualifier priority based on the table. Qualifier priority is from top to bottom.

If the Android operating system is looking for specific resources and cannot find them, it will try to find other resources. In this case, the Android operating system may not use the default resources. For example, if the operating system is looking for low-density pixel resources and cannot find them, it chooses medium or high-density pixel resources.

Imagine an application whose resources are as follows:

rawable
drawable-en
drawable-fr-rCA
drawable-en-port
drawable-en-notouch-12key
drawable-en-port-ldpi
drawable-port-ldpi
drawable-port-notouch-12key

This application is run with these resources on a device with the following features and facilities:

Screen density: hdpi
Locale: en-GB
Primary keyboard input method: 12key
Orientation: port
Touchscreen type: notouch

Then, French sources that do not conform to en-GB are removed as follows:

drawable
drawable-en
drawable-en-port
drawable-en-notouch-12key
drawable-en-port-ldpi
drawable-port-ldpi
drawable-port-notouch-12key

All resources that do not comply with the en-GB language will be removed.

After deleting resources that do not comply with the en-GB language, the available resources are as follows:

drawable-en-port
drawable-en-notouch-12key
drawable-en-port-ldpi

Xamarin. Android

Orientation is then performed and all directories that do not match the page orientation are removed:

drawable-en-port
drawable-en-port-ldpi
Then, directories that do not match the page density are removed:
drawable-en-port-ldpi

In this way, the Android operating system selects the appropriate source to use.

Source: https://www.dotnek.com/Blog/Apps/application-resources-available-in-xamarin-an